<?php

/**
 * contact : 刘经理 19326416371
 * company : 版权所属 四川北星惯牌科技有限公司
 **/

namespace app\common\manager;

use app\common\model\NumberStatistics;
use app\common\model\ProfitStatistics;
use app\commons\model\InviteStatistics;
use think\Db;
use think\Exception;

class StatisticsManager extends BaseManager
{
    CONST TYPE_INVITE = 1;
    CONST TYPE_NUMBER = 2;
    CONST TYPE_PROFIT = 3;

    public static function enter($merchantId,$storeId,$value,$date,$detail,$type = self::TYPE_INVITE,$trans=true):array
    {

        if ($trans)
            Db::startTrans();
        try {

            switch ($type) {
                case self::TYPE_INVITE:
                    $result = self::invite($merchantId,$storeId,$value,$date,false);
                    break;
                case self::TYPE_NUMBER:
                    $result = self::number($merchantId,$storeId,$value,$date,$detail,false);
                    break;
                case self::TYPE_PROFIT:
                    $result = self::profit($merchantId,$storeId,$value,$date,$detail,false);
                    break;
                default:
                    throw new Exception('当前类型不存在');
            }
            if ($result['code'] != self::$successCode) {
                throw new Exception($result['message']);
            }
            if ($trans)
                Db::commit();
            self::setStatusCode(self::$successCode);
            self::setMessage(self::$successMessage);
        }catch (Exception $e){
            if ($trans)
                Db::rollback();
            self::setStatusCode(self::$failCode);
            self::setMessage($e->getMessage());
        }
        return ['code'=>self::getStatusCode(),'message'=>self::getMessage()];
    }

    public static function invite($merchantId,$storeId,$value,$date,$trans=true):array
    {
        $inviteStatisticsModel = new InviteStatistics();
        if ($trans)
            Db::startTrans();
        try {
            $year = date('Y',$date);
            $month = date('m',$date);
            $day = date('d',$date);
            /**
             * 今日数据更新或插入
             */
            $dayInfo = $inviteStatisticsModel->where('time_type',InviteStatistics::TIME_TYPE_DAY)
                ->where('merchant_id',$merchantId)
                ->where('store_id',$storeId)
                ->where('year',$year)
                ->where('month',$month)
                ->where('day',$day)
                ->find();
            if (!$dayInfo){
                $insertId = $inviteStatisticsModel->insertGetId([
                    'time_type'=>InviteStatistics::TIME_TYPE_DAY,
                    'merchant_id'=>$merchantId,
                    'store_id'=>$storeId,
                    'year'=>$year,
                    'month'=>$month,
                    'day'=>$day,
                    'number'=>$value,
                    'create_time'=>time(),
                    'update_time'=>time(),
                ]);

                if (!$insertId){
                    throw new Exception('今日数据插入失败');
                }

            }else{
                if(!$inviteStatisticsModel->where('time_type',InviteStatistics::TIME_TYPE_DAY)
                    ->where('merchant_id',$merchantId)
                    ->where('store_id',$storeId)
                    ->where('year',$year)
                    ->where('month',$month)
                    ->where('day',$day)
                    ->update(['number'=>Db::raw('number + '.$value),'update_time'=>time()])){
                    throw new Exception('今日数据更新失败');
                }

            }

            /**
             * 当月数据更新或插入
             */
            $monthInfo = $inviteStatisticsModel->where('time_type',InviteStatistics::TIME_TYPE_MONTH)
                ->where('merchant_id',$merchantId)
                ->where('store_id',$storeId)
                ->where('year',$year)
                ->where('month',$month)
                ->find();

            if (!$monthInfo){
                $insertMonthId = $inviteStatisticsModel->insertGetId([
                    'time_type'=>InviteStatistics::TIME_TYPE_MONTH,
                    'merchant_id'=>$merchantId,
                    'store_id'=>$storeId,
                    'year'=>$year,
                    'month'=>$month,
                    'number'=>$value,
                    'create_time'=>time(),
                    'update_time'=>time(),
                ]);

                if (!$insertMonthId){
                    throw new Exception('本月数据插入失败');
                }
            }else{
                if(!$inviteStatisticsModel->where('time_type',InviteStatistics::TIME_TYPE_MONTH)
                    ->where('merchant_id',$merchantId)
                    ->where('store_id',$storeId)
                    ->where('year',$year)
                    ->where('month',$month)
                    ->update([
                        'number'=>Db::raw('number + '.$value),
                        'update_time'=>time(),
                    ])){
                    throw new Exception('本月数据更新失败');
                }
            }


            /**
             * 本年度数据更新或插入
             */
            $yearInfo = $inviteStatisticsModel->where('time_type',InviteStatistics::TIME_TYPE_YEAR)
                ->where('merchant_id',$merchantId)
                ->where('store_id',$storeId)
                ->where('year',$year)
                ->find();

            if (!$yearInfo){
                $insertYearId = $inviteStatisticsModel->insertGetId([
                    'time_type'=>InviteStatistics::TIME_TYPE_YEAR,
                    'merchant_id'=>$merchantId,
                    'store_id'=>$storeId,
                    'year'=>$year,
                    'number'=>$value,
                    'create_time'=>time(),
                    'update_time'=>time(),
                ]);

                if (!$insertYearId){
                    throw new Exception('本月数据插入失败');
                }
            }else{
                if(!$inviteStatisticsModel->where('time_type',InviteStatistics::TIME_TYPE_YEAR)
                    ->where('merchant_id',$merchantId)
                    ->where('store_id',$storeId)
                    ->where('year',$year)
                    ->update([
                        'number'=>Db::raw('number + '.$value),
                        'update_time'=>time(),
                    ])){
                    throw new Exception('本月数据更新失败');
                }
            }

            if ($trans)
                Db::commit();
            self::setStatusCode(self::$successCode);
            self::setMessage(self::$successMessage);
        }catch (Exception $e){
            if ($trans)
                Db::rollback();
            self::setStatusCode(self::$failCode);
            self::setMessage($e->getMessage());
        }
        return ['code'=>self::getStatusCode(),'message'=>self::getMessage()];
    }


    public static function number($merchantId,$storeId,$value,$date,$detail,$trans=true):array
    {
        $numberStatisticsModel = new NumberStatistics();
        if ($trans)
            Db::startTrans();
        try {
            $year = date('Y',$date);
            $month = date('m',$date);
            $day = date('d',$date);
            /**
             * 今日数据更新或插入
             */
            $dayInfo = $numberStatisticsModel->where('type',$detail)
                ->where('time_type',NumberStatistics::TIME_TYPE_DAY)
                ->where('merchant_id',$merchantId)
                ->where('store_id',$storeId)
                ->where('year',$year)
                ->where('month',$month)
                ->where('day',$day)
                ->find();
            if (!$dayInfo){
                $insertId = $numberStatisticsModel->insertGetId([
                    'type'=>$detail,
                    'time_type'=>NumberStatistics::TIME_TYPE_DAY,
                    'merchant_id'=>$merchantId,
                    'store_id'=>$storeId,
                    'year'=>$year,
                    'month'=>$month,
                    'day'=>$day,
                    'number'=>$value,
                    'create_time'=>time(),
                    'update_time'=>time(),
                ]);

                if (!$insertId){
                    throw new Exception('今日数据插入失败');
                }

            }else{
                if(!$numberStatisticsModel->where('type',$detail)
                    ->where('time_type',NumberStatistics::TIME_TYPE_DAY)
                    ->where('merchant_id',$merchantId)
                    ->where('store_id',$storeId)
                    ->where('year',$year)
                    ->where('month',$month)
                    ->where('day',$day)
                    ->update(['number'=>Db::raw('number + '.$value),'update_time'=>time()])){
                    throw new Exception('今日数据更新失败');
                }

            }

            /**
             * 当月数据更新或插入
             */
            $monthInfo = $numberStatisticsModel->where('type',$detail)
                ->where('time_type',NumberStatistics::TIME_TYPE_MONTH)
                ->where('merchant_id',$merchantId)
                ->where('store_id',$storeId)
                ->where('year',$year)
                ->where('month',$month)
                ->find();

            if (!$monthInfo){
                $insertMonthId = $numberStatisticsModel->insertGetId([
                    'type'=>$detail,
                    'time_type'=>NumberStatistics::TIME_TYPE_MONTH,
                    'merchant_id'=>$merchantId,
                    'store_id'=>$storeId,
                    'year'=>$year,
                    'month'=>$month,
                    'number'=>$value,
                    'create_time'=>time(),
                    'update_time'=>time(),
                ]);

                if (!$insertMonthId){
                    throw new Exception('本月数据插入失败');
                }
            }else{
                if(!$numberStatisticsModel->where('type',$detail)
                    ->where('time_type',NumberStatistics::TIME_TYPE_MONTH)
                    ->where('merchant_id',$merchantId)
                    ->where('store_id',$storeId)
                    ->where('year',$year)
                    ->where('month',$month)
                    ->update([
                        'number'=>Db::raw('number + '.$value),
                        'update_time'=>time(),
                    ])){
                    throw new Exception('本月数据更新失败');
                }
            }


            /**
             * 本年度数据更新或插入
             */
            $yearInfo = $numberStatisticsModel->where('type',$detail)
                ->where('time_type',NumberStatistics::TIME_TYPE_YEAR)
                ->where('merchant_id',$merchantId)
                ->where('store_id',$storeId)
                ->where('year',$year)
                ->find();

            if (!$yearInfo){
                $insertYearId = $numberStatisticsModel->insertGetId([
                    'type'=>$detail,
                    'time_type'=>NumberStatistics::TIME_TYPE_YEAR,
                    'merchant_id'=>$merchantId,
                    'store_id'=>$storeId,
                    'year'=>$year,
                    'number'=>$value,
                    'create_time'=>time(),
                    'update_time'=>time(),
                ]);

                if (!$insertYearId){
                    throw new Exception('本年度数据插入失败');
                }
            }else{
                if(!$numberStatisticsModel->where('type',$detail)
                    ->where('time_type',NumberStatistics::TIME_TYPE_YEAR)
                    ->where('merchant_id',$merchantId)
                    ->where('store_id',$storeId)
                    ->where('year',$year)
                    ->update([
                        'number'=>Db::raw('number + '.$value),
                        'update_time'=>time(),
                    ])){
                    throw new Exception('本年度数据更新失败');
                }
            }

            if ($trans)
                Db::commit();
            self::setStatusCode(self::$successCode);
            self::setMessage(self::$successMessage);
        }catch (Exception $e){
            if ($trans)
                Db::rollback();
            self::setStatusCode(self::$failCode);
            self::setMessage($e->getMessage());
        }
        return ['code'=>self::getStatusCode(),'message'=>self::getMessage()];
    }



    public static function profit($merchantId,$storeId,$value,$date,$detail,$trans=true):array
    {
        $profitStatisticsModel = new ProfitStatistics();
        if ($trans)
            Db::startTrans();
        try {
            $year = date('Y',$date);
            $month = date('m',$date);
            $day = date('d',$date);
            /**
             * 今日数据更新或插入
             */
            $dayInfo = $profitStatisticsModel->where('type',$detail)
                ->where('time_type',ProfitStatistics::TIME_TYPE_DAY)
                ->where('merchant_id',$merchantId)
                ->where('store_id',$storeId)
                ->where('year',$year)
                ->where('month',$month)
                ->where('day',$day)
                ->find();
            if (!$dayInfo){
                $insertId = $profitStatisticsModel->insertGetId([
                    'type'=>$detail,
                    'time_type'=>ProfitStatistics::TIME_TYPE_DAY,
                    'merchant_id'=>$merchantId,
                    'store_id'=>$storeId,
                    'year'=>$year,
                    'month'=>$month,
                    'day'=>$day,
                    'money'=>bcadd($value,0,2),
                    'create_time'=>time(),
                    'update_time'=>time(),
                ]);

                if (!$insertId){
                    throw new Exception('今日数据插入失败');
                }

            }else{
                if(!$profitStatisticsModel->where('type',$detail)
                    ->where('time_type',ProfitStatistics::TIME_TYPE_DAY)
                    ->where('merchant_id',$merchantId)
                    ->where('store_id',$storeId)
                    ->where('year',$year)
                    ->where('month',$month)
                    ->where('day',$day)
                    ->update(['money'=>Db::raw('money + '.$value),'update_time'=>time()])){
                    throw new Exception('今日数据更新失败');
                }

            }

            /**
             * 当月数据更新或插入
             */
            $monthInfo = $profitStatisticsModel->where('type',$detail)
                ->where('time_type',ProfitStatistics::TIME_TYPE_MONTH)
                ->where('merchant_id',$merchantId)
                ->where('store_id',$storeId)
                ->where('year',$year)
                ->where('month',$month)
                ->find();

            if (!$monthInfo){
                $insertMonthId = $profitStatisticsModel->insertGetId([
                    'type'=>$detail,
                    'time_type'=>ProfitStatistics::TIME_TYPE_MONTH,
                    'merchant_id'=>$merchantId,
                    'store_id'=>$storeId,
                    'year'=>$year,
                    'month'=>$month,
                    'money'=>bcadd($value,0,2),
                    'create_time'=>time(),
                    'update_time'=>time(),
                ]);

                if (!$insertMonthId){
                    throw new Exception('本月数据插入失败');
                }
            }else{
                if(!$profitStatisticsModel->where('type',$detail)
                    ->where('time_type',ProfitStatistics::TIME_TYPE_MONTH)
                    ->where('merchant_id',$merchantId)
                    ->where('store_id',$storeId)
                    ->where('year',$year)
                    ->where('month',$month)
                    ->update([
                        'money'=>Db::raw('money + '.$value),
                        'update_time'=>time(),
                    ])){
                    throw new Exception('本月数据更新失败');
                }
            }


            /**
             * 本年度数据更新或插入
             */
            $yearInfo = $profitStatisticsModel->where('type',$detail)
                ->where('time_type',ProfitStatistics::TIME_TYPE_YEAR)
                ->where('merchant_id',$merchantId)
                ->where('store_id',$storeId)
                ->where('year',$year)
                ->find();

            if (!$yearInfo){
                $insertYearId = $profitStatisticsModel->insertGetId([
                    'type'=>$detail,
                    'time_type'=>ProfitStatistics::TIME_TYPE_YEAR,
                    'merchant_id'=>$merchantId,
                    'store_id'=>$storeId,
                    'year'=>$year,
                    'money'=>bcadd($value,0,2),
                    'create_time'=>time(),
                    'update_time'=>time(),
                ]);

                if (!$insertYearId){
                    throw new Exception('本年度数据插入失败');
                }
            }else{
                if(!$profitStatisticsModel->where('type',$detail)
                    ->where('time_type',ProfitStatistics::TIME_TYPE_YEAR)
                    ->where('merchant_id',$merchantId)
                    ->where('store_id',$storeId)
                    ->where('year',$year)
                    ->update([
                        'money'=>Db::raw('money + '.$value),
                        'update_time'=>time(),
                    ])){
                    throw new Exception('本年度数据更新失败');
                }
            }

            if ($trans)
                Db::commit();
            self::setStatusCode(self::$successCode);
            self::setMessage(self::$successMessage);
        }catch (Exception $e){
            if ($trans)
                Db::rollback();
            self::setStatusCode(self::$failCode);
            self::setMessage($e->getMessage());
        }
        return ['code'=>self::getStatusCode(),'message'=>self::getMessage()];
    }

}